package com.student.view;

import com.student.utils.DBMysql;
import com.student.utils.trigger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;


public class ManageView extends JFrame {
    private JPanel mainPanel;
    private JTextField nameField;
    private JComboBox<String> genderBox;
    private JTextField ageField;
    private JTextField classField;
    private JTextField studentIdField;
    private JTextField deparmentField;
    private JTextField majorField;

    private JTextField deandserField;
    private JTextField textField;

    private JButton addButton;
    private JButton deleteButton;
    private JButton searchButton;
    private JButton ReviseButton;



    private JTable resultTable;
    private JScrollPane scrollPane;

    public ManageView() {
        // 设置窗口属性
        super("学生信息管理");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 600);
        setLocationRelativeTo(null); // 居中显示
        resultTable = new JTable();
        scrollPane = new JScrollPane(resultTable);

        // 创建主面板
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // 姓名
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("姓名:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        nameField = new JTextField(20);
        nameField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(nameField, gbc);

        // 性别
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("性别:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        genderBox = new JComboBox<>(new String[]{"男", "女"});
        mainPanel.add(genderBox, gbc);

        // 年龄
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("年龄:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        ageField = new JTextField(20);
        ageField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(ageField, gbc);

        // 班级
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("班级:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        classField = new JTextField(20);
        classField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(classField, gbc);

        // 学号
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("学号:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        studentIdField = new JTextField(20);
        studentIdField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(studentIdField, gbc);

        // 院系
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("院系:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.WEST;
        deparmentField = new JTextField(20);
        deparmentField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add( deparmentField, gbc);

        // 专业
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("专业:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.anchor = GridBagConstraints.WEST;
        majorField = new JTextField(20);
        majorField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(majorField, gbc);

        //删除和搜索框
        gbc.gridx = 0;
        gbc.gridy = 7;
        gbc.anchor = GridBagConstraints.EAST;
        mainPanel.add(new JLabel("请输入删除和搜索的学生学号或者班级号:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.anchor = GridBagConstraints.WEST;
        deandserField = new JTextField(20);
        deandserField.setPreferredSize(new Dimension(300, 30));
        mainPanel.add(deandserField, gbc);

        JPanel buttonPanel = new JPanel();

        /**
         * 添加学生信息
         */
        gbc.gridx = 0;
        gbc.gridy = 8;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        addButton = new JButton("添加学生");
        buttonPanel.add(addButton);
        //
        // 在构造函数或初始化方法中添加按钮监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String name = nameField.getText().trim();
                String gender = (String) genderBox.getSelectedItem();
                String age = ageField.getText().trim();
                String className = classField.getText().trim();
                String studentId = studentIdField.getText().trim();
                String deparmentId = deparmentField.getText().trim();
                String majorId = majorField.getText().trim();


                // 检查输入是否有效
                if (name.isEmpty() || gender.isEmpty() || ageField.getText().isEmpty() || className.isEmpty() || studentId.isEmpty()|| deparmentId.isEmpty()|| majorId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入所有字段", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
               if(DBMysql.insert("insert into s_student values(?,?,?,?,?,?,?)" ,name, gender, age, className, studentId,deparmentId,majorId)>0
                )
                {trigger.createTriggers();
                    JOptionPane.showMessageDialog(
                            null,       // 父组件，null表示使用默认的顶级窗口作为父组件
                            "添加成功", // 显示的消息
                            "提示",     // 对话框的标题
                            JOptionPane.INFORMATION_MESSAGE);// 消息类型，这里使用信息图标
                }

            }
        });

        /**
        * 删除学生信息
        */
        deleteButton = new JButton("删除学生");
        buttonPanel.add(deleteButton);
        // 在构造函数或初始化方法中添加按钮监听器
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String studentId = deandserField.getText().trim();

                // 检查输入是否有效
                if (studentId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if(DBMysql.delete("delete from s_student where number=? ", studentId)>0) {
                    JOptionPane.showMessageDialog(
                            null,       // 父组件，null表示使用默认的顶级窗口作为父组件
                            "删除成功", // 显示的消息
                            "提示",     // 对话框的标题
                            JOptionPane.INFORMATION_MESSAGE // 消息类型，这里使用信息图标
                    );
                }else{
                    JOptionPane.showMessageDialog(
                            null,       // 父组件，null表示使用默认的顶级窗口作为父组件
                            "删除失败", // 显示的消息
                            "提示",     // 对话框的标题
                            JOptionPane.INFORMATION_MESSAGE); // 消息类型，这里使用信息图标
                }
            }
        });

        /**
         * 修改学生信息
         */
        ReviseButton = new JButton("修改学生信息");
        buttonPanel.add(ReviseButton);
        // 在构造函数或初始化方法中添加按钮监听器
        ReviseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String studentId = deandserField.getText().trim();
                String nameId = nameField.getText().trim();
                String ageId = ageField.getText().trim();
                String class1tId = classField.getText().trim();
                String studentId2 = studentIdField.getText().trim();
                String departmentId = deparmentField.getText().trim();
                String majorId = majorField.getText().trim();
                //获取下拉框值
                ComboBoxModel<String> model = genderBox.getModel();
                String sex1 = model.getElementAt(0);

                // 检查输入是否有效
                if (studentId.isEmpty()||nameId.isEmpty()||ageId.isEmpty()||class1tId.isEmpty()||studentId2.isEmpty()||departmentId.isEmpty()||majorId .isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请检查输入是否为空", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{
                if(DBMysql.update("update s_student set xm = ?, sex = ?,age = ?,class = ?,Departments = ?,major =? where number=? ",nameId,sex1,ageId,class1tId,departmentId,majorId, studentId)>0)
                {
                    JOptionPane.showMessageDialog(
                            null,       // 父组件，null表示使用默认的顶级窗口作为父组件
                            "修改成功", // 显示的消息
                            "提示",     // 对话框的标题
                            JOptionPane.INFORMATION_MESSAGE);// 消息类型，这里使用信息图标
                } else{

                    JOptionPane.showMessageDialog(
                            null,       // 父组件，null表示使用默认的顶级窗口作为父组件
                            "修改失败", // 显示的消息
                            "提示",     // 对话框的标题
                            JOptionPane.INFORMATION_MESSAGE);// 消息类型，这里使用信息图标
                }
                }
                }

        });

/**
 * 搜索学生信息
 */

        searchButton = new JButton("搜索学生");
        buttonPanel.add(searchButton);
        // 在构造函数或初始化方法中添加按钮监听器
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String deandserId = deandserField.getText().trim();

                // 检查输入是否有效
                if (deandserId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{
                    if(DBMysql.query("select * from s_student where number=? ", deandserId)==null){
                        return;
                    }else{
                        ResultSet rs = DBMysql.query("select * from s_student where number=? ", deandserId);
                        try {
                            String name1 = rs.getString("xm");
                            String sex1 = rs.getString("sex");
                            String age1 = rs.getString("age");
                            String grade1 = rs.getString("class");
                            String number1 = rs.getString("number");
                            String major1 = rs.getString("major");
                            String department1 = rs.getString("Departments");
                            String message="姓名："+name1+"\n"+
                                    "性别："+sex1+"\n"+
                                    "年龄："+age1+"\n"+
                                    "班级："+grade1+"\n"+
                                    "学号："+number1+"\n"+
                                    "院系："+department1+"\n"+
                                    "专业："+major1;
                            JOptionPane.showMessageDialog(null, message, "程序指南", JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }



                }


        });

/**
 * 查询学生成绩
 */

        JButton scoreButton = new JButton("查询成绩");
        buttonPanel.add(scoreButton);
        // 在构造函数或初始化方法中添加按钮监听器
        scoreButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String deandserId = deandserField.getText().trim();

                // 检查输入是否有效
                if (deandserId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{

                        ResultSet rs = DBMysql.query("select * from s_score where number=? ", deandserId);
                        while(true){

                            try {
                            String name1 = rs.getString("xm");
                            String grade1 = rs.getString("grade");
                            String subject1 = rs.getString("subject");
                            String number1 = rs.getString("number");

                            String message="姓名："+name1+"\n"+
                                     "科目："+subject1+"\n"+
                                    "成绩："+grade1+"\n"+
                                    "学号："+number1;

                            JOptionPane.showMessageDialog(null, message, "程序指南", JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                            try {
                                if (!rs.next()) break;
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                    }

                }
            }

        });


/**
 * 跳转奖惩信息管理
 */
        JButton rewardButton = new JButton("奖惩信息管理");
        buttonPanel.add(rewardButton);
        // 在构造函数或初始化方法中添加按钮监听器
        rewardButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                rewardView rewardView = new rewardView();
                rewardView.setVisible(true);

            }
        });

/**
 * 创建视图查询各个学生的学号、姓名、班级、专业、院系；
 */
        JButton viewButton = new JButton("查看视图");
        buttonPanel.add(viewButton);
        // 在构造函数或初始化方法中添加按钮监听器
        viewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String deandserId = deandserField.getText().trim();

                // 检查输入是否有效
                if (deandserId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{
                    ResultSet rs = DBMysql.query("SELECT * FROM student_dept_info where student_number=? ", deandserId);
                        try {
                            String name1 = rs.getString("student_name");
                            String sex1 = rs.getString("student_sex");
                            String age1 = rs.getString("student_age");
                            String grade1 = rs.getString("student_class");
                            String number1 = rs.getString("student_number");
                            String major1 = rs.getString("student_major");
                            String department1 = rs.getString("department_name");
                            String depnumber1 = rs.getString("department_number");
                            String message="姓名："+name1+"\n"+
                                    "性别："+sex1+"\n"+
                                    "年龄："+age1+"\n"+
                                    "班级："+grade1+"\n"+
                                    "学号："+number1+"\n"+
                                    "院系："+department1+"\n"+
                                    "专业："+major1+"\n"+
                                    "院系号："+depnumber1;

                            JOptionPane.showMessageDialog(null, message, "程序指南", JOptionPane.INFORMATION_MESSAGE);
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }

                    }

                }


        });
/**
 * 创建存储过程查询指定学生的成绩单
 */
        JButton saveButton = new JButton("存储过程查看成绩");
        buttonPanel.add(saveButton);
        // 在构造函数或初始化方法中添加按钮监听器
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框中的信息
                String deandserId = deandserField.getText().trim();

                // 检查输入是否有效
                if (deandserId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{
                    String url = "jdbc:mysql://localhost:3306/db_student";
                    String user = "root";
                    String password = "hsp";

                    try (Connection conn = DriverManager.getConnection(url, user, password)) {


                        // 创建CallableStatement来调用存储过程
                        CallableStatement cStmt = conn.prepareCall("{ CALL GetStudentGrades(?) }");

                        // 设置IN参数
                        cStmt.setString(1, deandserId);

                        // 执行存储过程
                        ResultSet rs = cStmt.executeQuery();

                        // 处理结果集
                        while (rs.next()) {
                            String subject = rs.getString("subject");
                            String grade = rs.getString("grade");
                            String message="学科："+subject+"\n"+
                                    "成绩："+grade;
                            JOptionPane.showMessageDialog(null, message, "程序指南", JOptionPane.INFORMATION_MESSAGE);
                        }

                        // 关闭资源
                        rs.close();
                        cStmt.close();
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                }

            }


        });
        /**
         * 创建触发器当增加、删除学生和修改学生班级信息时自动修改相应班级学生人数；逻辑添加在添加学生删除学生按钮里面
         *查询班级人数
         */
        JButton triggerButton = new JButton("查询班级人数");
        buttonPanel.add(triggerButton);
        // 在构造函数或初始化方法中添加按钮监听器
        triggerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String deandserId = deandserField.getText().trim();

                // 检查输入是否有效
                if (deandserId.isEmpty()) {
                    JOptionPane.showMessageDialog(ManageView.this, "请输入班级号", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }else{
                    ResultSet rs = DBMysql.query("SELECT * FROM s_class where classname=? ", deandserId);
                    try {
                        String number1 = rs.getString("num");
                        JOptionPane.showMessageDialog(null,"班级人数为："+number1, "程序指南", JOptionPane.INFORMATION_MESSAGE);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                }

            }


        });

        mainPanel.add(buttonPanel, gbc);
        // 添加主面板到窗口
        add(mainPanel);
        setVisible(true);
    }





    public static void main(String[] args) {
        new ManageView();
    }
}